#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <string.h>
#include <vector>
#include <time.h>
#include "Terminos.h"
#include "Documents.h"
#include "Vector.h"
#include "Matrix.h"


using namespace std;

#pragma GCC diagnostic ignored "-Wwrite-strings"


int main(int argc, char *argv[])
{
	/*time_t now = time(0);
    string dt = ctime(&now);
	cout << dt << endl;*/
	string query, repositorio;
	query.clear();
	repositorio.clear();

	for(int i=0; i<argc; i++)
   {

		if ((i<argc) && (strcmp(argv[i],"-r") == 0  ))
		{
			i++;
			while( (i<argc)&&(strcmp(argv[i],"-q") != 0) )
		   {
				repositorio+=argv[i];
				repositorio+=" ";
				i++;
		   }
		};

		if ((i<argc) && (strcmp(argv[i],"-q") == 0  ))
		{
		   i++;
		   while( (i<argc) && (strcmp(argv[i],"-r") != 0) )
		   {
				query+=argv[i];
				query+=" ";
				i++;
		   }

		};


   }


	if ((repositorio.length()==0) || (query.length()==0))
	{
		if(repositorio.length() ==0)
			printf("Se requiere nombre del repositorio\n");

		if(query.length() ==0)
			printf("Se requiere una consulta como parametro\n");
	}
	else
	{

		repositorio = "./Archivos/"+repositorio.substr(0,repositorio.length()-1);
		string archTermino = repositorio+"/palabras.txt";
		Terminos *Consulta = new Terminos(archTermino);
		Documents *Documentos = new Documents(repositorio+"/Documentos.txt");
		vector<double> Q;
		vector<string> terminosString;


		Consulta->ProsesarTerminos(query,"SumaFrec");
		Q=Consulta->getFrecVector(0);
		terminosString=Consulta->getTermVector();
		//Consulta->guardarEnArchivo(repositorio+"/frecconsulta.txt");
		delete Consulta;

        Vector* auxQuery = new Vector(&Q);
        Matrix* sXt = new Matrix(repositorio+"/sxt.txt","read");
        Vector* queryVector = auxQuery->vectorByMatrix(sXt);
        delete auxQuery;
        delete sXt;
        vector<double> auxlog = queryVector->getVec();

    	/*fstream archivito;
    	string archivitodir=repositorio+"/qConver.txt";
    	archivito.open (archivitodir.c_str(),ios::out);
        for(unsigned int i = 0;i<auxlog.size();i++)
        {
        	archivito<<auxlog[i]<<" ";

        }
        archivito.close();*/
        //Si hay algo en la consulta calculo el rank, sino listo los archivos.
        time_t now = time(0);
        string dt = ctime(&now);
        if(queryVector->size()>0)
        {
			Matrix* D = new Matrix(repositorio+"/D.txt","read");
			Vector* auxCosines = queryVector->matrixCosine(D);
			delete D;
			delete queryVector;
			vector<double> ranks = auxCosines->getVec();
			delete auxCosines;

			Documentos->cargarRanks(ranks);
			Documentos->imprimirRanksOrdenados(repositorio+"/listadoRanks"+dt+".txt");
        }
        else
        {
		Documentos->imprimirRanks(repositorio+"/listadoRanks"+dt+".txt","asc");
        }

        //printf("Finish\n");
        delete Documentos;

	}
}
